<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/2/16 14:18
 * Remark:
 */

namespace App\Http\Middleware\Approval;

use App\Consts\Approval\ApprovalConsts;
use App\Consts\BaseConsts\BaseConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Func\TokenFuc;
use App\Models\Staff\StaffModel;
use App\Tools\DingTalk\DingTalkDeptUserFacade;
use Closure;
use Illuminate\Http\Exceptions\HttpResponseException;


class ApprovalMiddleware
{
    private StaffModel $_staffModel;

    public function __construct()
    {
        $this->_staffModel = new StaffModel();
    }

    public function handle($request, Closure $next)
    {
        $currentUserInfo = TokenFuc::getCurrentStaffInfo();
        // 校验审批参数是否合法
        $approvals= $request->post('approvers_v2');
        foreach($approvals as $key => $approval){
            if($approval['taskAction_type'] == ApprovalConsts::APPROVAL_TYPE_NONE){
                // 如果为普通审批 判断用户是否存在或是否绑定好钉钉
                $this->checkApprovalStaffNone($approval['user_ids']);
            }else{
                $this->checkApprovalStaffNotNone($approval['user_ids']);
            }
        }
        return $next($request);
    }


    public function checkApprovalStaffNotNone($staffIds){
        $staffArray = explode(',',$staffIds);
        $staffList = $this->_staffModel->getStaffsInfo($staffArray,[
            'staff_id',
            'staff_name',
            'staff_dingding_account',
            'status'
        ]);
        foreach($staffList as $staffDetail){
            $this->extracted($staffDetail);
        }
    }

    public function checkApprovalStaffNone(string $staffId){
        $staffDetail = $this->_staffModel->getStaffDetail($staffId,[
            'staff_id',
            'staff_name',
            'staff_dingding_account',
            'status'
        ]);
        $this->extracted($staffDetail);
    }

    /**
     * @param mixed $staffDetail
     */
    public function extracted(mixed $staffDetail): void
    {
        if(!$staffDetail){
            throw new HttpResponseException(ResponseFunc::returnJson(
                'error',
                '',
                "数据异常，员工不存在或已删除",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }

        if ($staffDetail['status'] != BaseConsts::STATUS_NORMAL) {
            throw new HttpResponseException(ResponseFunc::returnJson(
                'error',
                '',
                "员工" . $staffDetail['staff_name'] . "不存在或已删除",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
        if (!isset($staffDetail['staff_dingding_account']) || !$staffDetail['staff_dingding_account']) {
            throw new HttpResponseException(ResponseFunc::returnJson(
                'error',
                '',
                "员工" . $staffDetail['staff_name'] . "暂未绑定钉钉",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
        DingTalkDeptUserFacade::getUserInfoByUserId($staffDetail['staff_dingding_account']);
    }

}